package impl

import (
	"adam2/internal/model"
	"anubis-framework/pkg/io"
)

type Real9QuantAccountLogDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetReal9QuantAccountLogDaoImpl() *Real9QuantAccountLogDaoImpl {
	return &Real9QuantAccountLogDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (r *Real9QuantAccountLogDaoImpl) FindTableName() string {
	return "REAL9_Q_ACCOUNT_LOG"
}

// 插入记录
func (r *Real9QuantAccountLogDaoImpl) Insert(everyDate string, accountName string) {
	r.db.Exec("insert into real9_q_account_log(DATE_, ACCOUNT_NAME, HOLD_STOCK_NUMBER, STOCK_ASSETS, "+
		"CAPITAL_ASSETS, TOTAL_ASSETS, TOTAL_STAMP_DUTY, TOTAL_REGISTRATE_FEE_WHEN_BUY, TOTAL_COMMISSION_WHEN_BUY, "+
		"TOTAL_REGISTRATE_FEE_WHEN_SELL, TOTAL_COMMISSION_WHEN_SELL) "+
		"select to_date(?, 'yyyy-mm-dd'), t.ACCOUNT_NAME, t.HOLD_STOCK_NUMBER, t.STOCK_ASSETS, t.CAPITAL_ASSETS, t.TOTAL_ASSETS, t.TOTAL_STAMP_DUTY, "+
		"t.TOTAL_REGISTRATE_FEE_WHEN_BUY, t.TOTAL_COMMISSION_WHEN_BUY, t.TOTAL_REGISTRATE_FEE_WHEN_SELL, t.TOTAL_COMMISSION_WHEN_SELL "+
		"from real9_q_account t where t.account_name = ?",
		everyDate, accountName)
}

// 计算简单移动平均线
func (r *Real9QuantAccountLogDaoImpl) WriteMa() {
	io.Infoln("计算简单移动平均线")

	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_FIVE")
	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_TEN")
	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_TWENTY")
	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_SIXTY")
	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_ONEHUNDREDTWENTY")
	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.CALCULATE_TWOHUNDREDFIFTY")
}

// 计算所有股票的乖离率
func (r *Real9QuantAccountLogDaoImpl) WriteBias() {
	io.Infoln("计算所有股票的乖离率")

	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.WRITE_BIAS")
}

// 计算某一天所有股票的简单移动平均线
func (r *Real9QuantAccountLogDaoImpl) WriteMaByDate(date string) {
	io.Infoln("开始计算某一天所有股票的简单移动平均线，日期[%s]", date)

	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.WRITE_MOVING_AVERAGE_BY_DATE(?)", date)
}

// 按照日期，计算所有股票在某一日的乖离率
func (r *Real9QuantAccountLogDaoImpl) WriteBiasByDate(date string) {
	io.Infoln("开始按照日期，计算所有股票在某一日的乖离率，日期[%s]", date)

	r.db.Exec("CALL PKG_REAL9_Q_ACCOUNT_LOG.WRITE_BIAS_BY_DATE(?)", date)
}

// 根据日期查找记录
func (r *Real9QuantAccountLogDaoImpl) FindByDate(date string) model.Real9QuantAccountLogArray {
	var real9QuantAccountLogArray model.Real9QuantAccountLogArray
	r.db.Raw("select * from real9_q_account_log t where t.date_=to_date(?, 'yyyy-mm-dd')", date).Scan(&real9QuantAccountLogArray)
	return real9QuantAccountLogArray
}

// 查询某个账户，前一日的总资产
func (r *Real9QuantAccountLogDaoImpl) FindLastTotalAssetsByAccountNameAndDate(accountName string, date string) float64 {
	var bullShortLine float64
	r.db.Raw("select t1.total_assets from (select * from real9_q_account_log t where t.account_name=? and t.date_<to_date(?, 'yyyy-mm-dd') order by t.date_ desc) t1 "+
		"where rownum<=1", accountName, date).Scan(&bullShortLine)
	return bullShortLine
}

// 根据日期删除记录
func (r *Real9QuantAccountLogDaoImpl) DeleteByDate(date string) {
	io.Infoln("根据日期[%s]删除记录", date)

	r.db.Exec("delete from real9_q_account_log t where t.date_=to_date(?,'yyyy-mm-dd')", date)
}